//
//  HSYCustomBaseTableViewMoveCell.m
//  高仿暴走斗图
//
//  Created by key on 16/3/29.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYCustomBaseTableViewMoveCell.h"
#import "UIView+Gesture.h"

@interface HSYCustomBaseTableViewMoveCell ()

@property (nonatomic, assign) CGPoint beganPonint;
@property (nonatomic, assign) CGPoint lastPoint;
@property (nonatomic, assign) BOOL isMoving;
@property (nonatomic, strong) UIPanGestureRecognizer *panGesture;

@end
@implementation HSYCustomBaseTableViewMoveCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = [UIColor whiteColor];
        _slidingWith = -(IPHONE_WIDTH/2);
        
    }
    return self;
}

- (void)addPanGestureRecognizerInView:(UIView *)view {
    
    if (self.panGesture) {
        return;
    }
    //添加滑动手势
    self.panGesture = [UIView addGestureRecognizerWithGesturesType:kGesturesTypePanGestureRecognizer action:@selector(panGestureRecognizer:) delegate:self];
    self.panGesture.delegate = self;
    [view addGestureRecognizer:self.panGesture];
    self.beganPonint = CGPointMake(0, 0);//初始化位置
}

- (void)removePanGestureRecognizerForView:(UIView *)view {
    
    if (!self.panGesture) {
        return;
    }
    [view removeGestureRecognizer:self.panGesture];
}

#pragma mark - Gesture recognizer delegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && self.directionType != kSlideCellRevealDirectionTypeNone) {
        //滑动类型
        UIPanGestureRecognizer *panGestureRecognizer = (UIPanGestureRecognizer *)gestureRecognizer;
        CGPoint translation = [panGestureRecognizer translationInView:[self superview]];
        return (fabs(translation.x) / fabs(translation.y) > 1) ? YES : NO;
        
    } else {
        
        return NO;
    }
}

- (void)panGestureRecognizer:(UIPanGestureRecognizer *)panGesture {
    
    //移动距离获取
    self.lastPoint = [panGesture translationInView:self.contentView];
    switch (panGesture.state) {
            
        case UIGestureRecognizerStateEnded:{
            
            @weakify(self);
            if (self.contentView.viewX > (self.slidingWith)/2) {
                
                [UIView animateWithDuration:[HSYCustomBaseTableViewMoveCell animationDuration] animations:^{
                    //右滑动移动cell
                    @strongify(self);
                    [self.contentView setViewPoint:CGPointMake(0,0)];
                    
                }];
                self.isMoving = NO;
                
            } else if (self.contentView.viewX <= (self.slidingWith)/2) {
                
                [UIView animateWithDuration:[HSYCustomBaseTableViewMoveCell animationDuration] animations:^{
                    
                    @strongify(self);
                    [self.contentView setViewPoint:CGPointMake(self.slidingWith,0)];
                    
                } completion:^(BOOL finished) {
                    
                    @strongify(self);
                    self.isMoving = YES;
                    //委托（动作结束）
                    [self didDelegateForGestureStatus:kCellGestureStatusTypeEndOpen];
                }];
                
            }
            //获取移动结束时的位置坐标
            self.beganPonint = CGPointMake(self.contentView.viewX, 0);
        }
            break;
            
        case UIGestureRecognizerStateBegan: {
            
            [self didDelegateForGestureStatus:kCellGestureStatusTypeShouldOpen];
        }
            break;
            
        case UIGestureRecognizerStateCancelled: {
            
            @weakify(self);
            if (self.contentView.viewX > (self.slidingWith)/2) {
                
                [UIView animateWithDuration:[HSYCustomBaseTableViewMoveCell animationDuration] animations:^{
                    
                    @strongify(self);
                    [self.contentView setViewPoint:CGPointMake(0,0)];
                    
                } completion:^(BOOL finished) {
                    
                    @strongify(self);
                    self.isMoving = NO;
                    
                }];
                
            } else if (self.contentView.viewX <= (self.slidingWith)/2) {
                
                [UIView animateWithDuration:[HSYCustomBaseTableViewMoveCell animationDuration] animations:^{
                    
                    @strongify(self);
                    [self.contentView setViewPoint:CGPointMake(self.slidingWith,0)];
                    
                } completion:^(BOOL finished) {
                    
                    @strongify(self);
                    self.isMoving = YES;
                    //委托（动作结束）
                    [self didDelegateForGestureStatus:kCellGestureStatusTypeEndOpen];
                }];
            }
        }
            break;
            
        case UIGestureRecognizerStateChanged: {
            
            CGFloat toLeft = self.beganPonint.x + self.lastPoint.x;
            CGFloat toRight = CGRectGetMidX(self.contentView.frame);
            
            if (toLeft <= self.slidingWith && self.lastPoint.x<0) {
                return;//左滑
            } else if (toRight >= 0 && self.lastPoint.x>0) {
                return;//右滑
            }
            NSLog(@"move: %f", self.beganPonint.x + self.lastPoint.x);
            [self.contentView setViewPoint:CGPointMake(toLeft, 0)];
        }
            break;
            
        default:
            break;
    }

}


#pragma mark - Delegate To VC 

- (void)didDelegateForGestureStatus:(kCellGestureStatusType)gestureStatus {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didCellMoveForGestureStatus:withObject:)]) {
        
        [self.delegate didCellMoveForGestureStatus:gestureStatus withObject:self];
    }
}



#pragma mark - Close Move

- (void)closeMoveCellWithAnimated:(BOOL)isAnimated {
    
    @weakify(self);
    if (isAnimated) {
        
        [UIView animateWithDuration:[HSYCustomBaseTableViewMoveCell animationDuration] animations:^{
            
            @strongify(self);
            [self.contentView setViewPoint:CGPointMake(0,0)];
            
        } completion:^(BOOL finished) {
            
            @strongify(self);
            self.isMoving = NO;
            
        }];
    } else {
        
        [self.contentView setViewPoint:CGPointMake(0,0)];
    }
}


#pragma mark - Animation Time 

+ (CGFloat)animationDuration {
    
    return 0.5f;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
